import type { MetricDescription } from './types';

import { reactive } from 'vue';

const toPercentage = (value: number) => {
    return Number(value * 100).toFixed(2);
};

const toDateTime = (time: number) => {
    const date = new Date(time * 1000); // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
    const Y = `${date.getFullYear()}-`;
    const M = `${date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1}-`;
    const D = `${date.getDate()} `;
    const h = `${date.getHours()}:`;
    const m = `${date.getMinutes()}:`;
    const s = date.getSeconds();
    return Y + M + D + h + m + s;
};
const toTime = (time: number) => {
    let hour;
    let minute;
    let result = '';
    if (time > 3600 * 24) {
        const day = time / (3600 * 24);
        time = time % (3600 * 24);
        hour = time / 3600;
        result = `${Number.parseInt(`${day}`)}天${Number.parseInt(`${hour}`)}时`;
    } else if (time > 3600) {
        hour = time / 3600;
        minute = (time % 3600) / 60;
        result = `${Number.parseInt(`${hour}`)}时${Number.parseInt(`${minute}`)}分`;
    } else if (time > 60) {
        minute = time / 60;
        const sec = time % 60;
        result = `${Number.parseInt(`${minute}`)}分${Number.parseInt(`${sec}`)}秒`;
    } else {
        result = `${time}秒`;
    }
    return result;
};

const toSize = (value: number) => {
    return Number(value / 1_048_576).toFixed(3);
};

export const desc = reactive<Record<string, MetricDescription>>({
    'system.cpu.count': { desc: 'CPU数量', unit: '核' },
    'system.cpu.usage': { desc: '系统CPU使用率', unit: '%', convert: toPercentage },
    'process.start.time': { desc: '程序启动时间', convert: toDateTime },
    'process.uptime': { desc: '程序已运行时长', convert: toTime },
    'process.cpu.usage': { desc: '当前进程CPU使用率', convert: toPercentage, unit: '%' },
    'jvm.memory.max': { desc: 'JVM 最大内存', unit: 'MB', convert: toSize },
    'jvm.memory.committed': { desc: 'JVM 可用内存', unit: 'MB', convert: toSize },
    'jvm.memory.used': { desc: 'JVM 已用内存', unit: 'MB', convert: toSize },
    'jvm.buffer.memory.used': { desc: 'JVM 缓冲区已用内存', unit: 'MB', convert: toSize },
    'jvm.buffer.count': { desc: '当前缓冲区数量', unit: '个' },
    'jvm.threads.daemon': { desc: 'JVM 守护线程数量', unit: '个' },
    'jvm.threads.live': { desc: 'JVM 当前活跃线程数量', unit: '个' },
    'jvm.threads.peak': { desc: 'JVM 峰值线程数量', unit: '个' },
    'jvm.classes.loaded': { desc: 'JVM 已加载 Class 数量', unit: '个' },
    'jvm.classes.unloaded.count': { desc: 'JVM 未加载 Class 数量', unit: '个' },
    'jvm.gc.memory.allocated.count': { desc: 'GC 时, 年轻代分配的内存空间', unit: 'MB', convert: toSize },
    'jvm.gc.memory.promoted.count': { desc: 'GC 时, 老年代分配的内存空间', unit: 'MB', convert: toSize },
    'jvm.gc.max.data.size': { desc: 'GC 时, 老年代的最大内存空间', unit: 'MB', convert: toSize },
    'jvm.gc.live.data.size': { desc: 'FullGC 时, 老年代的内存空间', unit: 'MB', convert: toSize },
    'jvm.gc.pause.count': { desc: '系统启动以来GC 次数', unit: '次' },
    'jvm.gc.pause.total_time': { desc: '系统启动以来GC 总耗时', unit: '秒' },
    'jvm.gc.pause.max': { desc: '系统启动以来GC 最大耗时', unit: '秒' },
    'tomcat.sessions.created.count': { desc: 'Tomcat已创建Session数', unit: '个' },
    'tomcat.sessions.expired.count': { desc: 'Tomcat已过期Session数', unit: '个' },
    'tomcat.sessions.active.current': { desc: 'Tomcat当前活跃Session数', unit: '个' },
    'tomcat.sessions.active.max': { desc: 'Tomcat活跃Session数峰值', unit: '个' },
    'tomcat.sessions.rejected.count': { desc: '超过Session最大配置后，拒绝的Session个数', unit: '个' },
    'tomcat.global.sent.count': { desc: '发送的字节数', unit: 'Byte' },
    'tomcat.global.request.max': { desc: '全局Request请求最长耗时', unit: '秒' },
    'tomcat.global.request.count': { desc: '全局Request请求次数', unit: '次' },
    'tomcat.global.request.total_time': { desc: '全局Request请求总耗时', unit: '秒' },
    'tomcat.servlet.request.max': { desc: 'Servlet请求最长耗时', unit: '秒' },
    'tomcat.servlet.request.count': { desc: 'Servlet总请求次数', unit: '次' },
    'tomcat.servlet.request.total_time': { desc: 'Servlet请求总耗时', unit: '秒' },
    'tomcat.threads.current': { desc: 'Tomcat 当前线程数(包括守护线程)', unit: '个' },
    'tomcat.threads.config.max': { desc: 'Tomcat配置的线程最大数', unit: '个' },
});
